package com.cyl.XmlDemo.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cyl.XmlDemo.dao.UserDao;
import com.cyl.XmlDemo.entity.Register;
import com.cyl.XmlDemo.entity.User;

@Service
public class UserServiceImpl implements UserService {
	
	private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private PasswordService passwordService;
	
	@Autowired
	private DefaultHashService hashService;
	
	@Autowired
	private UserDao userDao;

	@Override
	public String register(Register register) {
		//进行账号查重
		/*Integer duplicateName = userDao.duplicateChecking(register.getUsername());
		if(duplicateName.intValue() == 0){
			//加密后写入数据库
			String encryptedValue = passwordService.encryptPassword(register.getPassword());
			register.setPassword(encryptedValue);
			userDao.register(register);
			return "success";
		}
		return "error";*/
		Integer duplicateName = userDao.duplicateChecking(register.getUsername());
		if(duplicateName.intValue() == 0){
			//加密后写入数据库
			HashRequest request = new HashRequest.Builder()  
		            .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes(register.getPassword()))  
		            .setSalt(ByteSource.Util.bytes("123")).setIterations(2).build();
			String hex = hashService.computeHash(request).toHex();
			register.setPassword(hex);
			userDao.register(register);
			//TODO 登录系统
//			User user = new User();
//			user.setName(register.getUsername());
//			user.setPassword(hex);
//			Subject subject = SecurityUtils.getSubject();
//			UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());  
//			  
//		    try {  
//		        //登录  
//		        subject.login(token);  
//		    } catch (AuthenticationException e) { 
//		    	logger.error("登录失败", e);
//		    } 
			return "success";
		}
		return "error-username-duplicate";
	}

	@Override
	public String login(User user) {
		logger.debug("登录");
		HashRequest request = new HashRequest.Builder()  
	            .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes(user.getPassword()))  
	            .setSalt(ByteSource.Util.bytes("123")).setIterations(2).build();
		String hex = hashService.computeHash(request).toHex();
		user.setPassword(hex);
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());  
		  
	    try {  
	        //4、登录，即身份验证  
	        subject.login(token);  
	    } catch (AuthenticationException e) { 
	    	logger.error("登录失败", e);
	    }  
		return null;
	}

}
